import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import { signOut, getUserDetail } from '../api/api'
import Message from './module/Message'
export default new Vuex.Store({
    state: {
        record: [],
        UUID: '',
        token: '',
        currentPoint: [],
        user: null,

    },
    mutations: {
        currentPoint(state, newPoint){
            state.currentPoint = newPoint;
        },
        add(state, payload) {
            state.record.push(payload);
        },
        clear(state, num) {
            state.record.splice(0, num);
        },
        set_token(state, payload) {
            state.token = payload.token
            state.UUID = payload.UUID
            state.user = payload.user
            localStorage.token = payload.token
            localStorage.UUID = payload.UUID
            localStorage.user = payload.user
            sessionStorage.UUID = payload.UUID
            sessionStorage.user = payload.user
        },
        del_token(state) {
            state.token = ''
            state.UUID = ''
            state.userGroup = ''
            state.user = null
            localStorage.removeItem('token')
            localStorage.removeItem('UUID')
            localStorage.removeItem('user')
            sessionStorage.removeItem('user')
            sessionStorage.removeItem('userGroup')
            sessionStorage.removeItem('UUID')
        },
        set_user(state, usermsg) {
            state.user = JSON.stringify(usermsg)
            sessionStorage.removeItem('user')
            localStorage.removeItem('user')
            localStorage.user = JSON.stringify(usermsg)
            sessionStorage.user = JSON.stringify(usermsg)
        }
    },
    actions: {
        incrementAsync({ commit, state, dispatch }, products) {
            commit('increment');
        },
        getUserMesAsync({ commit, state, dispatch }) {
            getUserDetail({
                token: state.token,
                UUID: state.UUID
            }).then(res => {
                if (process.env.NODE_ENV === "development") {
                  console.log(res);
                }
                const tmpList = {...JSON.parse(state.user), ...res}
                commit('set_user', tmpList)
            });
        }
    },
    getters: {
        doneTodos: (state, getters) => {
            return state.todos.filter(todo => todo.done);
        }
    },
    modules: {
        Message
    }
})